<?php

declare(strict_types=1);

use Hyperf\ModelCache\Handler\RedisHandler;

$poolConfig = [
    'min_connections' => 1,
    'max_connections' => 10,
    'connect_timeout' => 10.0,
    'wait_timeout' => 3.0,
    'heartbeat' => -1,
    'max_idle_time' => (float)env('DB_MAX_IDLE_TIME', 60),
];

function genConfigure($name = 'default'): array
{
    $dbGroup = ($name === 'default') ? '' : $name . '/';
    return [
        'path' => 'models/' . $dbGroup,
        'force_casts' => true,
        'inheritance' => '\App\Abstract\Model',
        'uses' => '',
        'table_mapping' => [],
    ];
}

function cacheConfigure(string $prefix, int $ttl = 3600 * 2, string $cacheKey = 'DB:%s:%s:%s:%s'): array
{
    return [
        'handler' => RedisHandler::class,
        'cache_key' => $cacheKey,
        'prefix' => $prefix,
        'ttl' => $ttl,
        'empty_model_ttl' => 5,
        'load_script' => true,
        'use_default_value' => false,
    ];
}

return [
    'default' => [
        'driver' => env('DB_DRIVER', 'mysql'),
        'host' => env('DB_HOST', 'localhost'),
        'port' => env('DB_PORT', 3306),
        'database' => env('DB_DATABASE', 'hyperf'),
        'username' => env('DB_USERNAME', 'root'),
        'password' => env('DB_PASSWORD', ''),
        'charset' => env('DB_CHARSET', 'utf8mb4'),
        'collation' => env('DB_COLLATION', 'utf8mb4_unicode_ci'),
        'prefix' => env('DB_PREFIX', ''),
        'pool' => $poolConfig,
        'commands' => ['gen:model' => genConfigure()],
        'cache' => cacheConfigure('db_main')
    ]
];
